//
//  MDHHTTPService.h
//  MDHPT
//
//  Created by 高得华 on 2019/4/23.
//  Copyright © 2019年 高得华. All rights reserved.
//

#if __has_include(<AFNetworking/AFNetworking.h>)
#import <AFNetworking/AFNetworking.h>
#else
#import "AFNetworking.h"
#endif
#import "RACSubscriber+AFProgressCallbacks.h"
#import "MDHHTTPRequest.h"

#if DEBUG
///测试环境miaomiao-api2.emibuy.com
static NSString * _Nullable const kHttpBaseUrl = @"http://chengziapi.emibuy.com/";
//static NSString * const kHttpBaseUrl = @"http://open2.gamemm.com/";
static BOOL const isFormalTest = true;
///测试的H5页面的网址
static NSString * _Nullable const kMMWBaseWebUrl = @"http://miaomiao-tp5.emibuy.com/";
///Socket 地址
static NSString * _Nullable const kMDHSocketUrl = @"ws://120.27.5.202:2348";
#else
///正式环境
static NSString * _Nullable const kHttpBaseUrl = @"http://chengziapi.xiguayuyin.com/";
static BOOL const isFormalTest = false;
///正式的H5页面的网址
static NSString * _Nullable const kMMWBaseWebUrl = @"http://h5.gamemm.com/";
///Socket 地址
static NSString * _Nullable const kMDHSocketUrl = @"wss://socket.gamemm.com:2348";
#endif


///用户信息
@class MDHUserModel, MDHHTTPResponse, MDHPTModel;

NS_ASSUME_NONNULL_BEGIN

@interface MDHHTTPService : AFHTTPSessionManager

///单例
+ (instancetype)sharedInstance;

///Token 请求接口的标识
@property (nonatomic, copy  ) NSString * token;
///当前的版本
@property (nonatomic, copy  ) NSString * version;

///存储用户
- (void)saveUser:(MDHUserModel *)user;
///删除用户
- (void)deleteUser:(MDHUserModel *)user;
///获取当前用户
- (MDHUserModel *)currentUser;
///获取当前用户的id
- (NSString *)currentUserId;
///保存 当前用户的钻石
- (void)saveDiamondMoney:(NSString *)money;
///保存 当前用户的金币
- (void)saveGoldMoney:(NSString *)money;

/// 用户信息配置完成
- (void)postUserDataConfigureCompleteNotification;
///是否登录
- (BOOL)isLogin;
///用户登录
- (void)loginUser:(MDHUserModel *)user;
///退出登录
- (void)logoutUser;

///当前的数据是否需要缓存 缓存并且返回缓存数据
///当前的数据是否存入缓存
@property (nonatomic, assign) BOOL isSetCache;
///当前的数据是否需要取出缓存 这是 返回的数据 会刷新两边  最好只是第一页并且是首次调用时设置为Yes
@property (nonatomic, assign) BOOL isGetCache;

@end

//MARK: - ==== 请求类
@interface MDHHTTPService (Request)

/// 1. 使用须知：后台返回数据的保证为👇固定格式 且`data:{}`必须为`字典`或者`NSNull`;
/// {
///    code：0,
///    msg: "",
///    data:{
///    }
/// }
/// 这个方法返回的 signal 将会 send `MHHTTPResponse`这个实例，`parsedResult`就是对应键data对应的值， 如果你想获得里面的parsedResult实例，请使用以下方法
/// [[self enqueueRequest:request resultClass:SBUser.class] sb_parsedResults];
/// 这样取出来的就是 SBUser对象

/// 2.使用方法如下
/*
 /// 1. 配置参数
 SBKeyedSubscript *subscript = [SBKeyedSubscript subscript];
 subscript[@"page"] = @1;
 
 /// 2. 配置参数模型
 SBURLParameters *paramters = [SBURLParameters urlParametersWithMethod:@"GET" path:SUProduct parameters:subscript.dictionary];
 
 /// 3. 创建请求
 /// 3.1 resultClass 传入对象必须得是 MHObject的子类
 /// 3.2 resultClass 传入nil ，那么回调回来的值就是，服务器返回来的数据
 [[[[MHHTTPRequest requestWithParameters:paramters]
 enqueueResultClass:[SBGoodsData class]]
 sb_parsedResults]
 subscribeNext:^(SBGoodsData * goodsData) {
 /// 成功回调
 
 } error:^(NSError *error) {
 /// 失败回调
 
 } completed:^{
 /// 完成
 
 }];
 
 */

/**
 Enqueues a request to be sent to the server.
 This will automatically fetch a of the given endpoint. Each object
 from each page will be sent independently on the returned signal, so
 subscribers don't have to know or care about this pagination behavior.
 
 @param request config the request
 @param resultClass A subclass of `MHObject` that the response data should be returned as,
 and will be accessible from the `parsedResult`
 @return Returns a signal which will send an instance of `MHHTTPResponse` for each parsed
 JSON object, then complete. If an error occurs at any point,
 the returned signal will send it immediately, then terminate.
 */
-(RACSignal *)enqueueRequest:(MDHHTTPRequest *) request
                 resultClass:(Class /*subclass of MHObject*/) resultClass;


/**
 用来上传多个文件流，也可以上传单个文件
 
 @param request MHHTTPRequest
 @param resultClass 要转化出来的请求结果且必须是 `MHObject`的子类，否则Crash
 @param fileDatas 要上传的 文件数据，数组里面必须是装着` NSData ` 否则Crash
 @param name  这个是服务器的`资源文件名`，这个服务器会给出具体的数值，不能传nil 否则 Crach
 @param mimeType http://www.jianshu.com/p/a3e77751d37c 如果传nil ，则会传递 application/octet-stream
 @return Returns a signal which will send an instance of `MHHTTPResponse` for each parsed
 JSON object, then complete. If an error occurs at any point,
 the returned signal will send it immediately, then terminate.
 */
- (RACSignal *)enqueueUploadRequest:(MDHHTTPRequest *) request
                        resultClass:(Class /*subclass of MHObject*/) resultClass
                          fileDatas:(NSArray <NSData *> *)fileDatas
                               name:(NSString *)name
                           mimeType:(NSString *)mimeType;



+ (id)transPtModel:(MDHPTModel *)model;
+ (MDHPTModel *)transAnToModel:(id)model;


@end



NS_ASSUME_NONNULL_END
